package com.finance.logistics.anlyzier;


import com.finance.base.constant.BaseConstants;
import com.finance.framework.lang.Privilege;
import com.finance.report.bean.pojo.LogisticsData;
import com.finance.system.bean.pojo.LogisticsColumnDef;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;


/**
 * Created by CBWL on 2017/4/27.
 */
@Component
public class StatusHandler extends AbstractHandler {
    @Override
    @Resource(name="scheduleTypeHandler")
    public void setNextHandler(AbstractHandler nextHandler){
        super.setNextHandler(nextHandler);
    }
    @Override
    public void doHandler(LogisticsData logisticsData) {
        this.logger.info("待解析运单信息：{}", net.sf.json.JSONObject.fromObject(logisticsData).toString());
        Integer status = logisticsData.getStatus();
        LogisticsColumnDef logisticsColumnDef = this.logisticsMappingService.getByClasz(this.getClass().getCanonicalName());
        if(logisticsColumnDef == null){
            this.logger.error("LogisticsColumnDef is null 未知的运单列映射[clazz]:{}[运单号]：{}",new String[]{this.getClass().getCanonicalName(),logisticsData.getWaybillNum()});
            String errorReason = "未知的运单列映射[clazz]:{"+this.getClass().getCanonicalName()+"},[运单号]：{"+logisticsData.getWaybillNum()+"}";
            this.logisticsErrorLogService.addErrorLog(logisticsData,errorReason,BaseConstants.ANALYZE_ERROR_TYPE_STATUS);
        }else{
            int target = HandlerHelper.analyze(status,logisticsColumnDef);
            if(target==-1){
                this.logger.error("未知的运单列映射[direction]");
                String errorReason = "未知的运单列映射[direction]";
                this.logisticsErrorLogService.addErrorLog(logisticsData,errorReason,BaseConstants.ANALYZE_ERROR_TYPE_STATUS);
            }else{
                String tableName = this.logisticsDataService.getDataTableName(logisticsData.getSignTime());
                LogisticsData logisticsData2update = this.logisticsDataService.getByWaybillNum(tableName,logisticsData.getWaybillNum());
                logisticsData2update.setStatus(target);
                try {
                    this.logisticsDataService.update(logisticsData2update);
                    this.updateDataVild(logisticsData2update,Privilege.B8);
                    this.getNextHandler().doHandler(logisticsData);
                } catch (Exception e) {
                    logger.error(e.getMessage(),e);
                }
            }
        }
    }
}
